package org.abadirou.cdebacker.flst.entity;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;

import org.abadirou.cdebacker.flst.api.Chambre;
import org.abadirou.cdebacker.flst.api.OptionChambre;
import org.abadirou.cdebacker.flst.api.TypeChambre;


@NamedQueries({
		@NamedQuery(name = "ALL_CHAMBRE", query = "FROM Chambre ORDER BY numero DESC"),
		@NamedQuery(name = "CHAMBRE_BY_NUMERO", query = "FROM Chambre where numero = :numero"),
		@NamedQuery(name = "CHAMBRE_BY_ETAGE", query = "FROM Chambre where etage = :etage"),
		@NamedQuery(name = "CHAMBRE_BY_TYPE", query = "FROM Chambre where type = :type ") })
@Entity(name = "Chambre")
public class ChambreImpl implements Chambre {

	public static final String ALL_CHAMBRE = "ALL_CHAMBRE";
	public static final String CHAMBRE_BY_NUMERO = "CHAMBRE_BY_NUMERO";
	public static final String CHAMBRE_BY_ETAGE = "CHAMBRE_BY_ETAGE";
	public static final String CHAMBRE_BY_TYPE = "CHAMBRE_BY_TYPE";

	@Id
	@GeneratedValue(strategy = GenerationType.SEQUENCE)
	private Long id;

	private int numero;
	private int etage;
	
	@ManyToOne
	@JoinColumn(name = "type_id", nullable = false)
	private final TypeChambreImpl type;

	@ManyToMany(fetch=FetchType.LAZY)
	private List<OptionChambreImpl> optionByChambres = new ArrayList<OptionChambreImpl>();


	
	public ChambreImpl(){
		this.numero = 000;
		this.type = null;
		this.etage = numero / 100;
	}
	
	public ChambreImpl(int numero2, TypeChambreImpl type) {

		this.numero = numero2;
		this.type = type;
		this.etage = numero2 / 100;
	}

	@Override
	public Long getId() {
		return id;
	}

	@Override
	public Integer getNumero() {
		return numero;
	}

	@Override
	public Integer getEtage() {
		return etage;
	}

	@Override
	public TypeChambre getType() {
		return type;
	}
	
	@Override
	public List<? extends OptionChambre> getOptions() {
		return optionByChambres;
	}
	
	@Override
	public void addOption(OptionChambre option) {
		if ( !getOptions().contains(option)) {
			optionByChambres.add((OptionChambreImpl) option);
		}
	}

	@Override
	public void removeOptionChambre(OptionChambre option) {
		optionByChambres.remove(option);
	}

}
